const regex = /^[0-9]+.?[0-9]*$/;

cc.Class({
    extends: cc.Component,

    properties: {
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.isRuning = false;
        this.dispatchEvent(this.events['create'])

        this.node.on('onCollider',()=>{
            this.dispatchEvent(this.events['collide'])
        })
    },
    onDisable(){
        this.node.off('onCollider')
        // this.cb = null;
        this.options = null;
        this.isRuning = false;
        this.dispatchEvent(this.events['destroy'])
    },
    dispatchEvent(event){
        if(!event){
            return;
        }
        // var key = parseFloat(event);
        if(!event.chance || Math.random() < event.chance){
            this.cb && this.cb(event,this);
        }
        // if (key.toString() != "NaN") {
        // } else if(this.events[key]){
        //     this.cb && this.cb(key,this);
        // }
    },
    setOptions(options,cb){
        this.options = options;
        this.runTime = 0;
        this.isRuning = true;
        this.cb = cb

        this.timeEvent = [];
        this.events = {};
        for(var i=0; i<options.length; i++){
            var event = options[i].split(',');

            var key = parseFloat(event[0]);
            if (key.toString() != "NaN") {
                this.timeEvent.push({
                    time : key,
                    chance : event[1],
                    event: event[2],
                    repeatTime : (event.length >= 4 ? parseFloat(event[3]) : 0),
                    autoDestroy : (event.length >= 5 ? parseFloat(event[4]) : false)
                })
        　　}else{
                this.events[event[0]] = {
                    chance : event[1],
                    event:event[2],
                    autoDestroy : (event.length >= 3 ? parseFloat(event[3]) : false)
                };
            }
        }
    },

    update(dt){
        if(cc.isPauseGame || !this.timeEvent.length){
            return;
        }
        var i=0;
        var autoDestroy = false;
        while(i<this.timeEvent.length){
            var event = this.timeEvent[i];
            event.time -= dt;
            if(event.time <0){
                this.dispatchEvent(event);
                if(event.repeatTime){
                    event.time = event.repeatTime;
                } else {
                    this.timeEvent.splice(i,1);
                }
                autoDestroy = autoDestroy || event.autoDestroy
            } else {
                i++;
            }
        }
        if(autoDestroy){
            var ps = this.node.getComponent("ParticleSprite");
            this.node.removeComponent('EventsBullet')
            ps.recovery();
        }
    }
    


    // update (dt) {},
});
